/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  
	AE.dialogName = "accessext-form-list-abbr";	
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{
	displayErrorSums: function(targetclass, BP, std, notApplicable, note, bestPractices) {
		try {
			var stdmsg = "";
			var msg = "<h3>" + targetclass + "&nbsp;&nbsp;"
			if (BP != "") {msg += "<a class='ext' href='http://bestpractices.cita.uiuc.edu/html" + BP + "' title='" + bestPractices + ": " + targetclass + "'>" + bestPractices + "</a>";}
			msg += "</h3><ul>";
			for (var i = 0; i < dataarray.length; i++) {
				if (dataarray[i].objclass == targetclass && dataarray[i][std] != notApplicable && dataarray[i][std] != note) {
					stdmsg += "<li>" + dataarray[i].msg + "</li>";
					var percentage = Math.round((dataarray[i].frequency / dataarray[i].totalObjs) * 100);
				//	msg += '<li class="pass"><img src="chrome://accessext/content/css/images/pass.jpg"><span class="eval">' + dataarray[i][std] + ': </span>' + percentage + '% (' + dataarray[i].frequency + ' out of ' + dataarray[i].totalObjs + ')';
					stdmsg += '<li class="' + dataarray[i][std].toLowerCase() + '"><span class="eval">' + dataarray[i][std] //+ ': </span>' + percentage + '% (' + dataarray[i].frequency + ' out of ' + dataarray[i].totalObjs + ')';
				}
			}
			if (stdmsg == "") {msg += '<li class="disc">N/A: No elements were found.</li>'}
			return msg += stdmsg + "</ul>";
		} catch (ex) {
			accessext_consoleDump('displayErrorSums exception: ', ex);
		}
	},
	
	pageReportNav: function(currStd, notApplicable, note, bestPractices) {
		try {
			var msg = "<h2><a name=nav-anchor>Navigation</a></h2>";
			msg += displayErrorSums('Title', '/nav/title/', currStd, notApplicable, note, bestPractices);
			msg += displayErrorSums('Menu and Navigation Bars', '/nav/menu/', currStd, notApplicable, note, bestPractices);
			msg += displayErrorSums('Image Maps', '/text/maps/', currStd, notApplicable, note, bestPractices);
			msg += displayErrorSums('Headings', '/nav/heading/', currStd, notApplicable, note, bestPractices);
			if (currStd != 'icita07') {
				msg += displayErrorSums('Links', '/nav/link/', currStd, notApplicable, note, bestPractices);
			}
			msg += displayErrorSums('Forms', '/nav/form/', currStd, notApplicable, note, bestPractices);
			msg += displayErrorSums('Data Tables', '/nav/dtable/', currStd, notApplicable), note, bestPractices;
			msg += displayErrorSums('Frames', '/nav/frame/', currStd, notApplicable, note, bestPractices);
			return msg;
		} catch (ex) {
			accessext_consoleDump('pageReportNav exception: ', ex);
		}
	},
	
	pageTextEq: function(currStd, notApplicable, note, bestPractices) {
		try {
			var msg = "<h2><a name=texteq-anchor>Text Equivalents</a></h2>"
			msg += displayErrorSums('Images', '/text/links/', currStd, notApplicable, note, bestPractices);
			msg += displayErrorSums('Objects', '', currStd, notApplicable, note, bestPractices);
			return msg;
		} catch (ex) {
			accessext_consoleDump('pageTextEq exception: ', ex);
		}
	},
	
	pageScripting: function(currStd, notApplicable, note, bestPractices) {
		try {
			var msg = "<h2><a name=scripting-anchor>Scripting</a></h2>"
			msg += displayErrorSums('Events', '/script/', currStd, notApplicable, note, bestPractices);
			if (currStd == 'aria') {
				msg += displayErrorSums('ARIA Widget', '', currStd, notApplicable, note, bestPractices);
				msg += displayErrorSums('ARIA Widget Relationship', '', currStd, notApplicable, note, bestPractices);
			}
			return msg;
		} catch (ex) {
			accessext_consoleDump('pageScripting exception: ', ex);
		}
	},
	
	pageStyle: function(currStd, notApplicable, note, bestPractices) {
		try {
			var msg = "";
	
			if (currStd != 'icita07') {
				msg = "<h2><a name=style-anchor>Style</a></h2>"
				msg += displayErrorSums('Colour Contrast', '', currStd, notApplicable, note, bestPractices);
				msg += displayErrorSums('Text Flow', '', currStd, notApplicable, note, bestPractices);
				msg += displayErrorSums('Text Sizing', '', currStd, notApplicable, note, bestPractices);
			}
			return msg;
		} catch (ex) {
			accessext_consoleDump('pageStyle exception: ', ex);
		}
	},
	
	pageValidator: function(currStd, notApplicable, note, bestPractices) {
		try {
			var msg = "<h2><a name=validator-anchor>Validator</a></h2>"
			msg += displayErrorSums('W3C Specifications', '/standards/html/',currStd, notApplicable, note, bestPractices);
			return msg;
		} catch (ex) {
			accessext_consoleDump('pageValidator exception: ', ex);
		}
	},
	
	pageReportAsBrowserPage: function(){
		try {	
			var currStd = currStandard();
			var notApplicable = accessext_getBundleSingleString('msg.unenforced');
			var note = accessext_getBundleSingleString('msg.note');
			var bestPractices = accessext_getBundleSingleString('msg.bestpractices');
			
	
			var outputAll="";
			var documents = accessext_getDocuments(AE.thewindow,new Array());
			var doc = documents[0];
			var currentTime = new Date()
			var month = currentTime.getMonth() + 1
			var day = currentTime.getDate()
			var year = currentTime.getFullYear()
			var hours = currentTime.getHours()
			var minutes = currentTime.getMinutes()
			if (minutes < 10) {	minutes = "0" + minutes }
			var strAMPM = "AM";
			if(hours > 11){strAMPM ="PM"}
		
			var outputString = "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-us' lang='en-us' xmlns:x2='http://www.w3.org/TR/xhtml2'>";
			outputString += "<head><title>Accessibility Extension: Page Report</title>";
			outputString += "</head>";
			outputString += '<link rel="stylesheet" href="chrome://accessext/content/css/sinorcaish-screen.css" type="text/css" media="screen"/>';
			outputString += '<link rel="stylesheet" href="chrome://accessext/content/css/sinorcaish-print.css" type="text/css" media="print"/>';
			outputString += "<body>";
			
			outputString += '<div id="sidebar"><div><h2 class="hidden">Report Sections</h2><ul>';
			outputString += '<li>&nbsp;&nbsp;&nbsp;<a href="#nav-anchor">Navigation</a></li>';
			outputString += '<li>&nbsp;&nbsp;&nbsp;<a href="#texteq-anchor">Text Equivalents</a></li>';
			outputString += '<li>&nbsp;&nbsp;&nbsp;<a href="#scripting-anchor">Scripting</a></li>';
			if (currStd != 'icita07') {
				outputString += '<li>&nbsp;&nbsp;&nbsp;<a href="#style-anchor">Style</a></li>';
			}
			outputString += '<li>&nbsp;&nbsp;&nbsp;<a href="#validator-anchor">Validator</a></li>';
			outputString += '</ul></div></div>';
			
			outputString += '<div id="main"><div id="context"><table cellpadding="0" cellspacing="0"><tr><td class="context-1">Title: <strong>Post Data</strong></td><td class="context-2">Date: ';
			outputString += month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + strAMPM  + '</td></tr><tr><td>URL: ';
			outputString += doc.documentURI + '</td><td class="context-2">Rule Set: ' + currStd.replace('icita', 'FAE ') + '</td></tr></table></div>';  
	
			outputString += "<h1>Page Report - Accessibility Extension</h1>";
	
			outputString += pageReportNav(currStd, notApplicable, note, bestPractices);
			outputString += pageTextEq(currStd, notApplicable, note, bestPractices);
			outputString += pageScripting(currStd, notApplicable, note, bestPractices);
			outputString += pageStyle(currStd, notApplicable, note, bestPractices);
			outputString += pageValidator(currStd, notApplicable, note, bestPractices);
			
			outputString += "</div>";
			outputString += "</body>";
			try {
				var accessibleHTMLPage = window.open("Template.html", "AccessExt as broswer page", 'status=1,toolbar=1,scrollbars=1,menubar=1') 
			} catch (ex) {
				accessext_consoleDump('accessExtAsBrowserPage exception: ' + ex);
			}
		    
			function tryAgain() {
				var bodyElement = accessibleHTMLPage.document.getElementById("bodyID");
		        if (bodyElement) {
		 			bodyElement.innerHTML = outputString;
		        }
		        else {
		             setTimeout (tryAgain, 500);
		        }
			}
		    try {
		 		var bodyElement = accessibleHTMLPage.document.getElementById("bodyID");
				if (!(bodyElement)) {
		            throw "tantrum"; }
				else {
		 			bodyElement.innerHTML = outputString;
				}       
		    }
		    catch (ex) {
		        setTimeout (tryAgain, 500);
		        return;
		    }// need to wait until this loads	
		} catch (ex) {
			accessext_consoleDump('accessExtSummaryAsBrowserPage exception: ', ex);
		}
	}
});
}});